<?php
/**
 * 直接生成 HTML 文件 
 *
 * @filesource htmlfile.class.php
 * @package DsCache
 * @subpackage 
 * @version $id: 0.2, utf8, Tue Dec 15 21:04:44 CST 2009
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2009 MiFunny China Inc.
 * @link http://mifunny.info/
 * @example 
 *   
 */
class htmlfile extends datafile{
	/**
	 * 默认的缓存策略
	 *
	 * DSCACHE_LIFE_TIME - life_time: 缓存有效时间(秒), 默认值 900
     *    如果设置为 0 表示缓存总是失效,
     *    设置为 -1 则表示不检查缓存有效期.
	 *
	 * DSCACHE_UMASK - cache_dir_umask: 创建缓存目录的权限, 默认为 0700
     *
	 * @var array
	 */
	private $_default_policy = array(
		'life_time'         => 900,
		'cache_dir'         => null,
		'cache_dir_umask'   => 0755,
    );
    
    /**
	 * 构造函数
	 *
	 * @param 默认的缓存策略 $default_policy
	 */
	public function __construct(array $default_policy = null){
		if (!is_null($default_policy))
			$this->_default_policy = array_merge($this->_default_policy, $default_policy);
	}

    /**
     * 初始化 默认策略
     *
	 * @param 默认的缓存策略 $default_policy
     */    
	public function _init(array $default_policy = null){
		if (!is_null($default_policy)){
            $this->_default_policy = $this->_policy($default_policy);
        }else{
            throw new RuntimeException("Error: Give me my own policy please! (class:".__CLASS__.")");
        }
	}
	
    public function set($id, $data, $expiration=null){
        $path = $this->_path($id);
        file_put_contents($path, $data, LOCK_EX);
        clearstatcache();
        return true;
    }

    public function get($id, $expiration=null){
    	$life_time = is_null($expiration) ? $this->_default_policy['life_time'] : $expiration;
        $path = $this->_path($id);
        
        if( is_file($path) ){
            if( $life_time==0 OR
              (time()-filectime($path))<=$life_time ){
                return file_get_contents($path);   
            }else
                return false;
        }else
            return false;
    }

    protected function _path($id){
    	$path = pathinfo($id);		

        $real_dir = $this->_default_policy['cache_dir'];
        $path['dirname'] = trim( $path['dirname'], '\\/');
        if( $path['dirname'] != '.' OR !empty($path['dirname']) ){
            $real_dir .= DIRECTORY_SEPARATOR.$path['dirname'];
            //if( is_dir($real_dir) ) {
            //	$real_dir = rtrim($real_dir, '\\/');
            //}else{
            if( !is_dir($real_dir) ){
                echo '2 real_dir: '.$real_dir;
            	$real_dir = $this->_default_policy['cache_dir'];
            	 foreach( preg_split('/\/|\\\/', $path['dirname'], -1,PREG_SPLIT_NO_EMPTY) as $d ){
                 	$real_dir .= DIRECTORY_SEPARATOR.$d;
                    if( !is_dir($real_dir) ){
                    	//echo "<br/>\t\nReal_DIR: ".$real_dir;
                    	mkdir($real_dir, $this->_default_policy['cache_dir_umask']);
                    }
                 }//END foreach subdir
            }//NOT a dir
		}
		
        return $real_dir . DIRECTORY_SEPARATOR . $path['basename'];
    }
          
}//END class htmlfile
?>
